package com.wlyuan.open.domain.assembler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wlyuan.core.domain.MappingIds;
import com.wlyuan.core.marker.MappingStatus;
import com.wlyuan.open.domain.PushTask;
import com.wlyuan.open.domain.PushTaskStatus;
import com.wlyuan.open.domain.PushTenant;
import com.wlyuan.open.domain.mappings.MappingPush;
import com.wlyuan.open.vo.push.basic.CarrierDetailVO;
import com.wlyuan.open.vo.push.order.OrderDetailVO;
import com.wlyuan.open.vo.push.track.TrackDetailVO;
import com.wlyuan.open.vo.push.waybill.WaybillPickCovenantDetailVO;
import lombok.var;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.time.LocalDateTime;

public class PushTaskAssembler {
    @Mapper(uses = {
            MappingIds.class,
            MappingPush.class,
            MappingStatus.class
    },imports = {
            PushTaskStatus.class,
            LocalDateTime.class
    })
    interface Assembler {
        @Mapping(target = "id",ignore = true)
        @Mapping(target = "tenantId",ignore = true)
        @Mapping(target = "status",expression = "java(PushTaskStatus.CREATED)")
        @Mapping(target = "retryCount",expression = "java(0)")
        @Mapping(target = "requestUrl",source = "pushUrl")
        @Mapping(target = "createAt",expression = "java(LocalDateTime.now())")
        PushTask assemble(PushTenant pushTenant);
    }

    private static final Assembler ASSEMBLER = Mappers.getMapper(Assembler.class);

    public static PushTask merge(OrderDetailVO order, PushTenant pushTenant){
        var pushTask = ASSEMBLER.assemble(pushTenant);
        pushTask.setTenantId(order.getTenantId());
        pushTask.setContractNo(order.getContractNo());
        pushTask.setOrderId(order.getOrderId());
        var businessInfo = JSONObject.parseObject(JSON.toJSONString(order));
        pushTask.setBusinessInfo(businessInfo);
        return pushTask;
    }

    public static PushTask merge(TrackDetailVO track, PushTenant pushTenant){
        var pushTask = ASSEMBLER.assemble(pushTenant);
        pushTask.setTenantId(track.getTenantId());
        pushTask.setContractNo(track.getContractNo());
        pushTask.setOrderId(track.getOrderId());
        var businessInfo = JSONObject.parseObject(JSON.toJSONString(track));
        pushTask.setBusinessInfo(businessInfo);
        return pushTask;
    }

    public static PushTask merge(WaybillPickCovenantDetailVO pickCovenant,PushTenant pushTenant){
        var pushTask = ASSEMBLER.assemble(pushTenant);
        pushTask.setTenantId(pickCovenant.getTenantId());
        pushTask.setContractNo(pickCovenant.getContractNo());
        pushTask.setOrderId(pickCovenant.getOrderId());
        var businessInfo = JSONObject.parseObject(JSON.toJSONString(pickCovenant));
        pushTask.setBusinessInfo(businessInfo);
        return pushTask;
    }

    public static PushTask merge(CarrierDetailVO carrierDetailVO,PushTenant pushTenant){
        var pushTask = ASSEMBLER.assemble(pushTenant);
        pushTask.setTenantId(carrierDetailVO.getTenantId());
        pushTask.setContractNo("-");
        pushTask.setOrderId(-1L);
        var businessInfo = JSONObject.parseObject(JSON.toJSONString(carrierDetailVO));
        pushTask.setBusinessInfo(businessInfo);
        return pushTask;
    }
}
